#ifndef EXTRACTIONMANAGER_H
#define EXTRACTIONMANAGER_H

#include <QThread>
#include <QList>
#include <QVector>
#include <QQueue>
#include "GetAudioSignalInThread.h"
#include <iostream>
#include <QtAlgorithms>

struct Extraction{
        int start;
        int end;
};

struct WaitingExtractionInformation{
    int idCutterMethod;
    int position;
    int nSecsMaxBefore;
    int nSecsMaxAfter;
    float sensibility;
    int medianDataSize;
};

class ExtractionManager : public QThread{
    Q_OBJECT
    public:
    explicit ExtractionManager(QObject *parent = 0);
    void startInformationExtraction(QString videoFileName);
    void addNewExtractionFromSimpleMethod(int position, int nSecsBefore, int nSecsAfter);
    void addNewExtractionFromAutomaticMethod(int position, int nSecsMaxBefore, int nSecsMaxAfter, double sensibility, int medianDataSize);
    void run();
    bool isCalculatingExtraction();
    bool isCalculatingWaveform();

    //void setExtraction(Extraction extraction);
    Extraction getExtraction(int id);
    Extraction getLastExtraction();
    int getNExtractions();
    void removeExtraction(int id);
    void replaceExtraction(int indice, Extraction newExtraction);
    void appendExtraction(Extraction extraction);
    float getMedianAudioData(int idFrame, int sizeData);


    signals:
    void newExtractionAdded(int start, int end);
    void calculingExtraction();
    //void waveFormGot();

    public slots:
    void getInformationsAndEventuallyAddWaitingExtraction();
    void getInformations();
    void eventuallyAddWaitingExtraction();
    void waitAndEventuallyAddWaitingExtraction();
    void stopAndReinitialise();

    private:
    bool dontStop;
    int idCutterMethod;
    GetAudioSignalInThread *getAudioSignalInThread;
    int nSecsBefore;
    int nSecsAfter;
    double sensibility;
    int medianDataSize;
    int position;
    int fps;
    int nFrames;
    float *waveForm;
    QList<Extraction> *extractions;
    QQueue<WaitingExtractionInformation> *waitingExtractions;
    void _setSimpleCuttingMethodParams(int position, int nSecsBefore, int nSecsAfter);
    void _setAutomaticCuttingMethodParams(int position, int nMaxSecsBefore, int nMaxSecsAfter, double sensibility, int medianDataSize);
    void _setIdCutterMethod(int idCutterMethod);
    void _addExtractionInWaitingList(int idCutterMethod, int position, int nSecsBefore, int nSecsAfter, double sensibility = -1.0, int medianDataSize = -1);

};

#endif // EXTRACTIONMANAGER_H
